package com.huashizhaoxi.lionapp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huashizhaoxi.lionapp.entity.Notice;
import com.huashizhaoxi.lionapp.entity.Result;
import com.huashizhaoxi.lionapp.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * notice-part页面根据类型获取notice
     * @param type
     * @param pageNum
     * @return Result
     */
    @GetMapping("/catch/{type}/{pageNum}")
    public Result getNoticeByType(@PathVariable(value = "type") String type,@PathVariable(value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Notice> noticeList = noticeService.getNoticeByType(Integer.parseInt(type));
        if(noticeList!= null) {
            PageInfo list = new PageInfo(noticeList);
            Map map = new HashMap();
            map.put("list",list.getList());
            map.put("size",list.getSize());
            map.put("total",list.getTotal());
            return Result.success("获取公告列表成功", map);
        } else {
            return Result.error("获取公告列表失败");
        }
    }

    @GetMapping("/new/catch/{pageNum}")
    public Result getNewNotice(@PathVariable(value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Notice> noticeList = noticeService.getNewNotice();
        if(noticeList!= null) {
            PageInfo list = new PageInfo(noticeList);
            Map map = new HashMap();
            map.put("list",list.getList());
            map.put("size",list.getSize());
            map.put("total",list.getTotal());
            return Result.success("获取最新公告成功", map);
        } else {
            return Result.error("获取最新公告失败");
        }
    }

    // 搜索公告
    @GetMapping("/search/catch/{keyword}/{pageNum}")
    public Result searchNotice(@PathVariable(value = "keyword") String keyword,@PathVariable(value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Notice> noticeList = noticeService.searchNotice(keyword);
        if(noticeList!= null) {
            PageInfo list = new PageInfo(noticeList);
            Map map = new HashMap();
            map.put("list",list.getList());
            map.put("size",list.getSize());
            map.put("total",list.getTotal());
            return Result.success("搜索公告成功", map);
        } else {
            return Result.error("搜索公告失败");
        }
    }

    @PostMapping("/add")
    public Result addNotice(@RequestBody Notice notice) {
        // 获取当前日期并插入notice
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        notice.setPosttime(LocalDate.parse(dateString));

        int res = noticeService.addNotice(notice);
        if(res == 1) {
            return Result.success("添加公告成功",null);
        } else {
            return Result.error("添加公告失败");
        }
    }
}
